package com.dubbo.cp.consumer.controller;

import java.util.List;

import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dubbo.cp.IService.CourseService;
import com.dubbo.cp.entity.CourseEntity;

import com.dubbo.cp.uitls.Pager;

@Controller
@RequestMapping("/course")
public class CourseController  extends BaseController{

	@Reference
	private CourseService courseServiceImpl;
	private Pager<CourseEntity> pager;
	
	

	/*
	 * 查全部
	 */
	@RequestMapping(value="uList")
	public String courseList(Model model) {


		List<CourseEntity> useList = courseServiceImpl.getCourseList();
		model.addAttribute("useList", useList);
		System.out.println("useList"+useList);

		return "courseList";
	}
	/*
	 * 
	 * 分页查询用户信息
	 */
	@RequestMapping(value="findCourseByPage")
	public ModelAndView findByPage(Model model,String currentPage) {
		System.out.println("进入分页查询");
		int pageSize = Integer.parseInt(currentPage);
		if(pager==null) {
			pager = new Pager<CourseEntity>();
		}
		pager.setCurrentPage(pageSize);
		int length = pager.getCurrentPage();
		int recordSize = pager.getRecordSize();
		int start = (length-1)*recordSize;
		pager.setList(courseServiceImpl.getCourseList());
		pager.initTotalPage();
		List<CourseEntity>ulist = courseServiceImpl.getCourseByPage(start, pager.getRecordSize());
		model.addAttribute("ulist", ulist);
		model.addAttribute("pager", pager);
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("/courseList");
		return modelAndView;

	}


	/**
	 * 
	 * 
	 * 添加角色跳转
	 * @return
	 */
	@RequestMapping(value="inputCourse")
	public String inputcourse() {
		return "courseAdd";

	}
	
	/*
	 * 
	 */
	@PostMapping(value="insertCourse")
	public String insertcourse(@Valid CourseEntity courseEntity) {

		System.out.println("添加");
		System.out.println(courseEntity.getCourseName());
		courseServiceImpl.insertCourseEntity(courseEntity);

		return "redirect:findCourseByPage?currentPage=1";

	}
	 /**
     * 编辑跳转
     * @return
     */
	
	@RequestMapping(value="editCourse")
	public ModelAndView editcourse(Model model,@RequestParam int id) {
		
		System.out.println("id===="+id);
		
		CourseEntity courseEntity = courseServiceImpl.findCourseByCourseid(id);
		model.addAttribute("courseEntity",courseEntity);
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("/courseEdit");
		return modelAndView;
		
	}
	
	@PostMapping(value="updateCourse")
	public String updatecourse(CourseEntity courseEntity) {
		courseServiceImpl.updateCourseEntity(courseEntity);
		
		return "redirect:findCourseByPage?currentPage=1";
		
	}
	
	@RequestMapping(value="deleteCourse")
	public String deletecourse(@RequestParam int id) {
		System.out.println(id);
		courseServiceImpl.deleteCourseEntity(id);
		return "redirect:findCourseByPage?currentPage=1";
		
	}
}
